const {TopClient} = require("../lib/api/topClient");

const serverConfig = require('../server.config');
const fs = require('fs')

let client = new TopClient(serverConfig.clientConfig)

class TaoBao {
    /**
     * @desc 当 timeStamp 不为零时证明需要校验间隔时间，校验间隔时间的算法 当前时间戳减去上一个时间戳 大于最大间隔时间 则通过校验否则未通过校验
     * @type {{timeStamp: number, do(number): boolean}}
     */
    static timeCheck = {
        timeStamp: 0,
        maxBufferTime: 1000 * 60 * 2,
        do(maxBufferTime) {
            maxBufferTime = maxBufferTime || this.maxBufferTime
            console.log({maxBufferTime, timeStamp: this.timeStamp, now: new Date().getTime()})
            if ((new Date().getTime() - this.timeStamp) < maxBufferTime) {
                return false
            }

            this.timeStamp = new Date().getTime()
            return true
        },
        reset() {
            this.timeStamp = 0
            return true
        }
    }


    static searchByKeyWord(keyword, page_no, page_size, has_coupon, sort) {
        let filePath = './searchByKeyword.json'
        // 融合自身的参数与默认参数
        let paramsTemp = {
            q: keyword ? keyword : serverConfig.defaultParamsConfig.q,
            page_no: page_no ? page_no : serverConfig.defaultParamsConfig.page_no,
            page_size: page_size ? page_size : serverConfig.defaultParamsConfig.page_size,
            has_coupon: has_coupon ? has_coupon : serverConfig.defaultParamsConfig.has_coupon,
            sort: sort ? sort : serverConfig.defaultParamsConfig.sort
        }
        let params = {...serverConfig.defaultParamsConfig, ...paramsTemp}
        return new Promise((resolve, reject) => {
            // if (!TaoBao.timeCheck.do(1000 * 10)) return resolve(JSON.parse(fs.readFileSync(filePath).toString()))
            client.execute(serverConfig.apiConfig.filter(item => item.name === '物料搜索')[0].url, params, (err, data) => {
                if (err) return TaoBao.timeCheck.reset() && reject(err)
                // fs.writeFileSync(filePath, JSON.stringify(data), {encoding: 'utf8'})
                resolve(data)
            })
        })
    }

    /**
     *
     * @param page_no
     * @param page_size
     * @param q
     * @param has_coupon
     * @param sort
     * @param material_id  {string} 物料id 可以根据物料id列表.array切换
     * @param deviceInfo {object} 设备信息对象   通过serverConfig.getDeviceInfo 生成
     * @returns {Promise<unknown>}
     */
    static getRecommendList({page_no, page_size, q, has_coupon, sort, material_id, deviceInfo,}) {
        let filePath = './recommendList.json'
        // 融合自身的参数与默认参数
        let paramsTemp = {
            q: q ? q : serverConfig.defaultParamsConfig.q,
            page_no: page_no ? page_no : serverConfig.defaultParamsConfig.page_no,
            page_size: page_size ? page_size : serverConfig.defaultParamsConfig.page_size,
            has_coupon: has_coupon ? has_coupon : serverConfig.defaultParamsConfig.has_coupon,
            sort: sort ? sort : serverConfig.defaultParamsConfig.sort
        }

        // serverConfig.defaultParamsConfig.adzone_id = '111059250204'
        let params = {
            ...serverConfig.defaultParamsConfig,
            material_id,
            ...deviceInfo, ...paramsTemp,
        }
        console.log(params)
        return new Promise((resolve, reject) => {
            // if (!TaoBao.timeCheck.do(1000 * 30)) return resolve(JSON.parse(fs.readFileSync(filePath).toString()))
            client.execute(
                serverConfig.apiConfig.filter(item => item.name === '物料精选')[0].url,
                params
                , (err, data) => {
                    if (err) return TaoBao.timeCheck.reset() && reject(err)
                    // fs.writeFileSync(filePath, JSON.stringify(data), {encoding: 'utf8'})
                    resolve(data)
                })
        })
    }

    /**
     * @desc 没有权限 也不知道去哪里申请
     * @returns {Promise<unknown>}
     */
    static getMaterialIds() {
        return new Promise((resolve, reject) => {
            client.execute(serverConfig.apiConfig.filter(item => item.name === '物料id获取')[0].url, {}, (err, data) => {
                console.log(err)
                if (err) return reject(err)
                resolve(data)
            })
        })
    }
}

module.exports = exports = TaoBao
